package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.vioao.wechat.api.component.wxa.TemplateApi;
import com.github.vioao.wechat.bean.response.component.wxa.template.LibraryAddResponse;
import com.github.vioao.wechat.bean.response.component.wxa.template.LibraryListResponse;
import com.github.vioao.wechat.bean.response.component.wxa.template.TemplateListResponse;
import one.stand.service.ComponentService;
import one.stand.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: QIK
 * @CreateDate: 2019/5/13 18:46
 */
@Service
public class TemplateServiceImpl extends PublicBaseService implements TemplateService {
    @Autowired
    @Lazy
    ComponentService componentService;

    @Override
    public LibraryListResponse libraryList(String appId, Integer offset, Integer count) {
        String tokne=componentService.getAuthorizerToken(appId);
        LibraryListResponse response=  TemplateApi.libraryList(tokne,offset,count);
        log.info("模板列表：{}", JSON.toJSONString(response));
        return response;
    }
    @Override
    public LibraryAddResponse libraryAdd(String appId, String noticeId, Integer...code) {
        String token=componentService.getAuthorizerToken(appId);
        List<Integer> keywordIdList=new ArrayList<>();
        for(Integer id : code){
            keywordIdList.add(id);
        }
        LibraryAddResponse response= TemplateApi.libraryAdd(token,noticeId,JSON.toJSONString(keywordIdList));
        log.info("模板设置返回：{}",JSON.toJSONString(response));
        return response;
    }

    @Override
    public TemplateListResponse list(String appId, Integer offset, Integer count) {
        String token=componentService.getAuthorizerToken(appId);
        TemplateListResponse response=TemplateApi.list(token,offset,count);
        return response;
    }
}
